<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 3.8.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"github.com","root":"/","scheme":"Pisces","version":"7.7.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="jquery获取jsonJS里使用 $!{string} 获取原格式的字符串；   出现此种错误时，将${} 改为$!{} 使用 js 动态元素选择器123var i=1;var head=&quot;head&quot;+i;$(&quot;#head&quot;+head+&quot;&quot;); 使用js处理双击、选中事件jquery 处理方式1$(document.body).on(&apos;mouseup&apos;,&quot;#content&quot;,mouseUp);">
<meta name="keywords" content="JavaScript">
<meta property="og:type" content="article">
<meta property="og:title" content="js特殊用法">
<meta property="og:url" content="https://github.com/LBraveHeart/2019/03/12/js特殊用法/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="jquery获取jsonJS里使用 $!{string} 获取原格式的字符串；   出现此种错误时，将${} 改为$!{} 使用 js 动态元素选择器123var i=1;var head=&quot;head&quot;+i;$(&quot;#head&quot;+head+&quot;&quot;); 使用js处理双击、选中事件jquery 处理方式1$(document.body).on(&apos;mouseup&apos;,&quot;#content&quot;,mouseUp);">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="https://github.com/2019/03/12/js特殊用法/clipboard.png">
<meta property="og:image" content="https://github.com/2019/03/12/js特殊用法/clipboard1.png">
<meta property="og:updated_time" content="2019-06-04T01:45:02.763Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="js特殊用法">
<meta name="twitter:description" content="jquery获取jsonJS里使用 $!{string} 获取原格式的字符串；   出现此种错误时，将${} 改为$!{} 使用 js 动态元素选择器123var i=1;var head=&quot;head&quot;+i;$(&quot;#head&quot;+head+&quot;&quot;); 使用js处理双击、选中事件jquery 处理方式1$(document.body).on(&apos;mouseup&apos;,&quot;#content&quot;,mouseUp);">
<meta name="twitter:image" content="https://github.com/2019/03/12/js特殊用法/clipboard.png">

<link rel="canonical" href="https://github.com/LBraveHeart/2019/03/12/js特殊用法/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true
  };
</script>

  <title>js特殊用法 | Hexo</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <div>
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Hexo</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>

</nav>
  <div class="site-search">
    <div class="popup search-popup">
    <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocorrect="off" autocapitalize="off" placeholder="搜索..." spellcheck="false" type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result"></div>

</div>
<div class="search-pop-overlay"></div>

  </div>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content">
            

  <div class="posts-expand">
      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block " lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://github.com/LBraveHeart/2019/03/12/js特殊用法/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="liu666">
      <meta itemprop="description" content="仰面朝天，知此身渺渺，沧海一粟">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Hexo">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          js特殊用法
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2019-03-12 11:00:00" itemprop="dateCreated datePublished" datetime="2019-03-12T11:00:00+08:00">2019-03-12</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2019-06-04 09:45:02" itemprop="dateModified" datetime="2019-06-04T09:45:02+08:00">2019-06-04</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/html/" itemprop="url" rel="index"><span itemprop="name">html</span></a>
                </span>
            </span>

          <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>2.9k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="fa fa-clock-o"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>3 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h3 id="jquery"><a href="#jquery" class="headerlink" title="jquery"></a>jquery</h3><h4 id="获取json"><a href="#获取json" class="headerlink" title="获取json"></a>获取json</h4><p>JS里使用 <strong><em>$!{string}</em></strong> 获取原格式的字符串；</p>
<img src="/2019/03/12/js特殊用法/clipboard.png" title="图片1">
<img src="/2019/03/12/js特殊用法/clipboard1.png" title="图片2">
<p>出现此种错误时，将<em>${}</em> 改为<em>$!{}</em> 使用</p>
<h4 id="js-动态元素选择器"><a href="#js-动态元素选择器" class="headerlink" title="js 动态元素选择器"></a>js 动态元素选择器</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> i=<span class="number">1</span>;</span><br><span class="line"><span class="keyword">var</span> head=<span class="string">"head"</span>+i;</span><br><span class="line">$(<span class="string">"#head"</span>+head+<span class="string">""</span>);</span><br></pre></td></tr></table></figure>
<h3 id="使用js处理双击、选中事件"><a href="#使用js处理双击、选中事件" class="headerlink" title="使用js处理双击、选中事件"></a>使用js处理双击、选中事件</h3><h4 id="jquery-处理方式"><a href="#jquery-处理方式" class="headerlink" title="jquery 处理方式"></a>jquery 处理方式</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$(<span class="built_in">document</span>.body).on(<span class="string">'mouseup'</span>,<span class="string">"#content"</span>,mouseUp);</span><br></pre></td></tr></table></figure>
<h4 id="dom处理方式："><a href="#dom处理方式：" class="headerlink" title="dom处理方式："></a>dom处理方式：</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">document</span>.addEventListener(<span class="string">"dblclick"</span>, doubleClick, <span class="literal">true</span>);</span><br></pre></td></tr></table></figure>
<h4 id="释放鼠标处理函数"><a href="#释放鼠标处理函数" class="headerlink" title="释放鼠标处理函数"></a>释放鼠标处理函数</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">mouseUp</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> text = <span class="string">""</span>;</span><br><span class="line">    <span class="keyword">if</span> (<span class="built_in">window</span>.getSelection) &#123;</span><br><span class="line">        text = <span class="built_in">window</span>.getSelection().toString();</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="built_in">document</span>.selection &amp;&amp; <span class="built_in">document</span>.selection.type != <span class="string">"Control"</span>) &#123;</span><br><span class="line">        text = <span class="built_in">document</span>.selection.createRange().text;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (<span class="string">""</span> != text&amp;&amp;<span class="string">" "</span> != text) &#123;</span><br><span class="line">    	$(<span class="string">"#checkText"</span>).val(text);</span><br><span class="line">    	layer.msg(text);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="js计算"><a href="#js计算" class="headerlink" title="js计算"></a>js计算</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">parseInt</span>(<span class="number">5</span>/<span class="number">2</span>); 结果：<span class="number">2</span> 舍弃小数部分，取整数</span><br><span class="line"><span class="built_in">Math</span>.ceil(<span class="number">5</span>/<span class="number">2</span>); 结果：<span class="number">3</span> 向上取整，有小时就整数加一</span><br><span class="line"><span class="built_in">Math</span>.round(<span class="number">5</span>/<span class="number">2</span>);结果：<span class="number">3</span> 四舍五入</span><br><span class="line"><span class="built_in">Math</span>.floor(<span class="number">5</span>/<span class="number">2</span>);结果：<span class="number">2</span> 向下取整</span><br></pre></td></tr></table></figure>
<h3 id="js给指定一篇文章添加行号"><a href="#js给指定一篇文章添加行号" class="headerlink" title="js给指定一篇文章添加行号"></a>js给指定一篇文章添加行号</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getLineNum</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> $lineNum=$(<span class="string">"#lineNum"</span>);</span><br><span class="line"></span><br><span class="line">$lineNum.empty();</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> rowNum=<span class="built_in">Math</span>.round(<span class="built_in">document</span>.getElementById(<span class="string">"contentT"</span>).scrollHeight/<span class="built_in">parseFloat</span>($(<span class="string">"#contentT"</span>).css(<span class="string">"line-height"</span>)));</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">"$('.article fl').height():"</span>+<span class="built_in">document</span>.getElementById(<span class="string">"contentT"</span>).scrollHeight);</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">"parseFloat($('.article fl').css('line-height')):"</span>+<span class="built_in">parseFloat</span>($(<span class="string">"#contentT"</span>).css(<span class="string">"line-height"</span>)));</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(rowNum);</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">var</span> i=<span class="number">1</span>;i&lt;rowNum;i++)&#123;</span><br><span class="line">	<span class="keyword">var</span> flag=<span class="literal">false</span>;</span><br><span class="line">	<span class="keyword">if</span>(headNum&gt;<span class="number">1</span>)&#123;</span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">var</span> j=<span class="number">2</span>;j&lt;=headNum;j++)&#123;</span><br><span class="line">			<span class="keyword">var</span> headId=<span class="string">"head"</span>+j;</span><br><span class="line">			<span class="keyword">var</span> $headEle=$(<span class="string">"#"</span>+headId+<span class="string">""</span>);</span><br><span class="line">			<span class="keyword">var</span> offsetTop=$headEle.position().top;</span><br><span class="line">			<span class="keyword">var</span> headRow=<span class="built_in">Math</span>.round(offsetTop/<span class="built_in">parseFloat</span>($(<span class="string">"#contentT"</span>).css(<span class="string">"line-height"</span>)));</span><br><span class="line">			headRow=headRow+<span class="number">1</span>;</span><br><span class="line">			<span class="keyword">if</span>(headRow==i)&#123;</span><br><span class="line">				flag=<span class="literal">true</span>;</span><br><span class="line">				<span class="keyword">break</span>;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span>(flag)&#123;</span><br><span class="line">		<span class="keyword">if</span>(i%<span class="number">5</span>==<span class="number">0</span>||i==<span class="number">1</span>)&#123;</span><br><span class="line">			$lineNum.append(<span class="string">"&lt;br&gt;&lt;p&gt;"</span>+i+<span class="string">"&lt;/p&gt;"</span>);</span><br><span class="line">		&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">			$lineNum.append(<span class="string">"&lt;br&gt;&lt;br&gt;"</span>);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">		<span class="keyword">if</span>(i%<span class="number">5</span>==<span class="number">0</span>)&#123;</span><br><span class="line">			$lineNum.append(<span class="string">"&lt;p&gt;"</span>+i+<span class="string">"&lt;/p&gt;"</span>);</span><br><span class="line">		&#125;<span class="keyword">else</span> <span class="keyword">if</span>(i==<span class="number">1</span>)&#123;</span><br><span class="line">			$lineNum.append(<span class="string">"&lt;p&gt;"</span>+i+<span class="string">"&lt;/p&gt;"</span>);</span><br><span class="line">		&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">			$lineNum.append(<span class="string">"&lt;br&gt;"</span>);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;&#125;;</span><br></pre></td></tr></table></figure>
<h3 id="js对象转换"><a href="#js对象转换" class="headerlink" title="js对象转换"></a>js对象转换</h3><h4 id="jquery对象dom对象"><a href="#jquery对象dom对象" class="headerlink" title="jquery对象dom对象"></a>jquery对象<->dom对象</-></h4><h5 id="dom对象转换为jquery对象："><a href="#dom对象转换为jquery对象：" class="headerlink" title="dom对象转换为jquery对象："></a>dom对象转换为jquery对象：</h5><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> d=<span class="built_in">document</span>.getElementById(<span class="string">"id"</span>);	<span class="comment">//对象d为DOM对象</span></span><br><span class="line"><span class="keyword">var</span> s=$(d);	<span class="comment">//对象s为Jquery对象</span></span><br></pre></td></tr></table></figure>
<h5 id="jquery对象转换为dom对象："><a href="#jquery对象转换为dom对象：" class="headerlink" title="jquery对象转换为dom对象："></a>jquery对象转换为dom对象：</h5><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> d=<span class="built_in">document</span>.getElementById(<span class="string">"id"</span>);   <span class="comment">//对象d为DOM对象</span></span><br><span class="line"><span class="keyword">var</span> s=$(d);	<span class="comment">//对象s为Jquery对象</span></span><br><span class="line"><span class="keyword">var</span> dd=s.get(<span class="number">0</span>)	<span class="comment">//对象dd为DOM对象</span></span><br></pre></td></tr></table></figure>
<h3 id="js随机打乱数组"><a href="#js随机打乱数组" class="headerlink" title="js随机打乱数组"></a>js随机打乱数组</h3> <figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">randomsort</span>(<span class="params">a, b</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">Math</span>.random()&gt;<span class="number">.5</span> ? <span class="number">-1</span> : <span class="number">1</span>;</span><br><span class="line">    <span class="comment">//用Math.random()函数生成0~1之间的随机数与0.5比较，返回-1或1</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>];</span><br><span class="line">arr.sort(randomsort);</span><br></pre></td></tr></table></figure>
<h3 id="js从数组中获取指定个数的随机数据"><a href="#js从数组中获取指定个数的随机数据" class="headerlink" title="js从数组中获取指定个数的随机数据"></a>js从数组中获取指定个数的随机数据</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 从数组中随机取指定数量的数据</span><br><span class="line"> * @param arr 数据</span><br><span class="line"> * @param count 随机数量</span><br><span class="line"> * @returns &#123;*&#125;</span><br><span class="line"> */</span><br><span class="line">function getRandomArrayElements(arr, count) &#123;</span><br><span class="line">    var shuffled = arr.slice(0), i = arr.length, min = i - count, temp, index;</span><br><span class="line">    while (i-- &gt; min) &#123;</span><br><span class="line">        index = Math.floor((i + 1) * Math.random());</span><br><span class="line">        temp = shuffled[index];</span><br><span class="line">        shuffled[index] = shuffled[i];</span><br><span class="line">        shuffled[i] = temp;</span><br><span class="line">    &#125;</span><br><span class="line">    return shuffled.slice(min);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="js禁止双击"><a href="#js禁止双击" class="headerlink" title="js禁止双击"></a>js禁止双击</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;body onselectstart = &quot;return false&quot; style = &#123; -moz-user-select : none &#125;&gt;&lt;/body&gt;</span><br></pre></td></tr></table></figure>
<h3 id="js控制div里的滚动条"><a href="#js控制div里的滚动条" class="headerlink" title="js控制div里的滚动条"></a>js控制div里的滚动条</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$(<span class="string">'.dtcon'</span>).animate(&#123;<span class="attr">scrollTop</span>:<span class="string">'0px'</span>&#125;, <span class="number">200</span>);</span><br><span class="line"><span class="number">200</span>:滚动条移动至指定位置所需时间 ms</span><br></pre></td></tr></table></figure>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/JavaScript/" rel="tag"># JavaScript</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2019/03/08/html/" rel="prev" title="form表单外不同的按钮提交到不同的url">
      <i class="fa fa-chevron-left"></i> form表单外不同的按钮提交到不同的url
    </a></div>
      <div class="post-nav-item">
    <a href="/2019/03/12/inal/" rel="next" title="jfinal 文件上传特殊处理">
      jfinal 文件上传特殊处理 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  

  </div>


          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let activeClass = CONFIG.comments.activeClass;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#jquery"><span class="nav-number">1.</span> <span class="nav-text">jquery</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#获取json"><span class="nav-number">1.1.</span> <span class="nav-text">获取json</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#js-动态元素选择器"><span class="nav-number">1.2.</span> <span class="nav-text">js 动态元素选择器</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#使用js处理双击、选中事件"><span class="nav-number">2.</span> <span class="nav-text">使用js处理双击、选中事件</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#jquery-处理方式"><span class="nav-number">2.1.</span> <span class="nav-text">jquery 处理方式</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#dom处理方式："><span class="nav-number">2.2.</span> <span class="nav-text">dom处理方式：</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#释放鼠标处理函数"><span class="nav-number">2.3.</span> <span class="nav-text">释放鼠标处理函数</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#js计算"><span class="nav-number">3.</span> <span class="nav-text">js计算</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#js给指定一篇文章添加行号"><span class="nav-number">4.</span> <span class="nav-text">js给指定一篇文章添加行号</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#js对象转换"><span class="nav-number">5.</span> <span class="nav-text">js对象转换</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#jquery对象dom对象"><span class="nav-number">5.1.</span> <span class="nav-text">jquery对象dom对象</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#dom对象转换为jquery对象："><span class="nav-number">5.1.1.</span> <span class="nav-text">dom对象转换为jquery对象：</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#jquery对象转换为dom对象："><span class="nav-number">5.1.2.</span> <span class="nav-text">jquery对象转换为dom对象：</span></a></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#js随机打乱数组"><span class="nav-number">6.</span> <span class="nav-text">js随机打乱数组</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#js从数组中获取指定个数的随机数据"><span class="nav-number">7.</span> <span class="nav-text">js从数组中获取指定个数的随机数据</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#js禁止双击"><span class="nav-number">8.</span> <span class="nav-text">js禁止双击</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#js控制div里的滚动条"><span class="nav-number">9.</span> <span class="nav-text">js控制div里的滚动条</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">liu666</p>
  <div class="site-description" itemprop="description">仰面朝天，知此身渺渺，沧海一粟</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">63</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">17</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">55</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/LBraveHeart" title="GitHub → https://github.com/LBraveHeart"><i class="fa fa-fw fa-github"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:446109730@qq.com" title="E-Mail → mailto:446109730@qq.com" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">liu666</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    <span title="站点总字数">58k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">53 分钟</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io" class="theme-link" rel="noopener" target="_blank">Hexo</a> 强力驱动 v3.8.0
  </div>
  <span class="post-meta-divider">|</span>
  <div class="theme-info">主题 – <a href="https://pisces.theme-next.org" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a> v7.7.2
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>
<script src="/js/utils.js"></script><script src="/js/motion.js"></script>
<script src="/js/schemes/pisces.js"></script>
<script src="/js/next-boot.js"></script>



  




  <script src="/js/local-search.js"></script>












  

  

</body>
</html>
